/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.StampListingForm;
import com.ntb.entities.StampDutyAndRegistration;
import com.ntb.models.StampDutyAndRegistrationDAO;
import com.ntb.models.impl.StampDutyAndRegistrationDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class StampListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        StampListingForm stampListingForm = (StampListingForm) form;

        StampDutyAndRegistrationDAO stampDutyAndRegistrationDAO = new StampDutyAndRegistrationDAOImpl();

        String action = stampListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            stampDutyAndRegistrationDAO.delete(stampListingForm.getId());
            successMessages.add("1 SD Charge out of 1 successfully deleted.");
        } else if (("deleteStamps").equalsIgnoreCase(action)) {
            int[] ids = stampListingForm.getIds();
            int count = 0;

            int length = 0;

            try {
                length = ids.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int id : ids) {
                    stampDutyAndRegistrationDAO.delete(id);
                    count++;
                }
                successMessages.add(count + " SD Charge out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        String baseUrl = request.getRequestURI() + "?";

        int page = 1;

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<StampDutyAndRegistration> stampList = stampDutyAndRegistrationDAO.getList(page);

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        request.setAttribute("stampList", stampList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(stampDutyAndRegistrationDAO.getPageCount());
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
